#include("/views/layout/_layout.html")
#@layout("blog - " + blog.title)
#define main()
<div class="blog-main-left">
    <div class="article-detail shadow">
        <div class="article-detail-title">
            #(blog.title)
        </div>
        <div class="article-detail-info">
            <span><i class="fa fa-clock-o fa-lg"></i>&nbsp;#date(blog.createTime, "yyyy-MM-dd HH:mm")</span>
            <span><i class="fa fa-tag fa-lg"></i><a href="#(ctx)/blog/index?tag=#(blog.tag)">&nbsp;#(blog.tag)</a></span>
            #if(isLogin)
            <span><i class="fa fa-pencil-square-o fa-lg"></i><a href="#(ctx)/blog/form/#(blog.id)">&nbsp;编辑</a></span>
            <span><i class="fa fa-trash-o fa-lg"></i><a href="javascript:" class="delBlogLink">&nbsp;删除</a></span>
            #end
        </div>
        <div class="article-detail-content" id="blogContent">#(blog.content)</div>
    </div>
</div>

<div id="directory-content" class="directory-content">
    <div id="directory"></div>
</div>
#end

#define right()
#include("/views/layout/_right.html")
#end

#define css()
<link href="#(ctx)/static/css/content.css" rel="stylesheet">
#end

#define js()
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script src="//cdn.bootcss.com/instantclick/3.0.1/instantclick.min.js"></script>
<script type="text/javascript">
    layui.config({base: '/static/js/'}).use(['jquery', 'layer', 'api'], function () { //加载jquery模块
        var $ = layui.jquery, layer = layui.layer, api = layui.api;

        /*hljs.initHighlightingOnLoad();*/
        $('.delBlogLink').on('click', function () {
            layer.confirm('确定删除吗？', {icon: 3, title: '提示'}, function (index) {
                api.deleteBlog({"blog.id": '#(blog.id)'}, function (res) {
                    switch (res.code) {
                        case 0:
                            location.href = '/';
                            break;
                        default:
                            layer.msg("删除失败", {shift: 6});
                            break;
                    }
                });
                layer.close(index);
            });
        });

        $('#directory').html('');

        var postDirectoryBuild = function() {
            var postChildren = function children(childNodes, reg) {
                    var result = [], node, i, len,
                        isReg = typeof reg === 'object',
                        isStr = typeof reg === 'string';
                    for (i = 0, len = childNodes.length; i < len; i++) {
                        node = childNodes[i];
                        if ((node.nodeType === 1 || node.nodeType === 9) &&
                            (!reg || isReg && reg.test(node.tagName.toLowerCase()) ||
                                isStr && node.tagName.toLowerCase() === reg)) {
                            result.push(node);
                        }
                    }
                    return result;
                },
                createPostDirectory = function(article, directory, isDirNum) {
                    var contentArr = [], titleId = [], levelArr, root, level,
                        currentList, list, li, link, i, len;
                    levelArr = (function(article, contentArr, titleId) {
                        var titleElem = postChildren(article.childNodes, /^h\d$/),
                            levelArr = [], lastNum = 1, lastRevNum = 1, count = 0, guid = 1,
                            id = 'directory' + (Math.random() + '').replace(/\D/, ''), num, elem;
                        while (titleElem.length) {
                            elem = titleElem.shift();
                            contentArr.push(elem.innerHTML);
                            num = +elem.tagName.match(/\d/)[0];
                            if (num > lastNum) {
                                levelArr.push(1);
                                lastRevNum += 1;
                            } else if (num === lastRevNum || num > lastRevNum && num <= lastNum) {
                                levelArr.push(0);
                            } else if (num < lastRevNum) {
                                levelArr.push(num - lastRevNum);
                                lastRevNum = num;
                            }
                            count += levelArr[levelArr.length - 1];
                            lastNum = num;
                            elem.id = elem.id || (id + guid++);
                            titleId.push(elem.id);
                        }
                        if (count !== 0 && levelArr[0] === 1) levelArr[0] = 0;

                        return levelArr;
                    })(article, contentArr, titleId);
                    currentList = root = document.createElement('ul');
                    dirNum = [0];
                    for (i = 0, len = levelArr.length; i < len; i++) {
                        level = levelArr[i];
                        if (level === 1) {
                            list = document.createElement('ul');
                            if (!currentList.lastElementChild) {
                                currentList.appendChild(document.createElement('li'));
                            }
                            currentList.lastElementChild.appendChild(list);
                            currentList = list;
                            dirNum.push(0);
                        } else if (level < 0) {
                            level *= 2;
                            while (level++) {
                                if (level % 2) dirNum.pop();
                                currentList = currentList.parentNode;
                            }
                        }
                        dirNum[dirNum.length - 1]++;
                        li = document.createElement('li');
                        link = document.createElement('a');
                        link.href = '#' + titleId[i];
                        link.innerHTML = !isDirNum ? contentArr[i] : dirNum.join('.') + ' ' + contentArr[i] ;
                        li.appendChild(link);
                        currentList.appendChild(li);
                    }
                    directory.appendChild(root);
                };
            createPostDirectory(document.getElementById('blogContent'), document.getElementById('directory'), true);
        };
        postDirectoryBuild();
    });
</script>
<script data-no-instant>
    InstantClick.on('change', function (isInitialLoad) {
        var blocks = document.querySelectorAll('pre code');
        for (var i = 0; i < blocks.length; i++) {
            hljs.highlightBlock(blocks[i]);
        }
        if (isInitialLoad === false) {
            if (typeof ga !== 'undefined') ga('send', 'pageview', location.pathname + location.search);
        }
    });
    InstantClick.init();
</script>
#end